package com.doudou.collection.exception;

import com.doudou.collection.response.ResultVO;
import com.doudou.collection.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName GlobalExceptionHandler<br>
 * @Description 全局异常处理类<br>
 * @Author 李浩文<br>
 * @Date 2023/6/3 11:17<br>
 * @Version 1.0<br>
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 用户名或密码错误抛出的异常
     */
    @ExceptionHandler({InternalAuthenticationServiceException.class, BadCredentialsException.class})
    public ResultVO handleAuthenticationException(AuthenticationException e) {
        if (e instanceof InternalAuthenticationServiceException) {
            log.warn("用户名不存在!");
            return new ResultVO(StatusCode.USERNAME_ERROR);
        }
        log.warn("密码错误!");
        return new ResultVO(StatusCode.PASSWORD_ERROR);
    }
}










